package com.pdh.controller;

import com.pdh.bean.Message;
import com.pdh.mvc.ResponseBody;
import com.pdh.service.AdminService;
import com.pdh.util.JSONUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Author: 彭德华
 * @Date 2021-08-29 22:19
 */
public class AdminController {
    @ResponseBody("/admin/login.do")
    public String login(HttpServletRequest request, HttpServletResponse response) {
        //1.接参数
        String adminName = request.getParameter("admin_name");
        String password = request.getParameter("password");
        //2.调用service传参数 并获取结果
        Message msg = AdminService.login(adminName, password);
        String ip = request.getRemoteAddr();//ip等于请求对象地址
        //3.更新ip和登陆时间
        AdminService.updateLoginInformation(adminName,ip);
        //登陆成功了设置一个标记adminPhone封装到msg里面
        //使用过滤器使得不登录不允许访问某些页面,同时，还可在插入快递时获取到此adminPhone作为插入人手机号
        if(msg != null){//存储用户信息到会话里面就很关键
            request.getSession().setAttribute("adminInfoMsg",msg);
        }
        //4.将数据转换为json
        String json = JSONUtil.toJSON(msg);
        //5.将JOSN数据回复给ajax
        return json;
    }

}
